package egovframework.npqs.pqi.mobile.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.R;
import egovframework.npqs.pqi.mobile.activity.approve.ApproveDetail;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.QuarantineEnblocItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.DelaysayuListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.QuarantineDetailListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.QunCdItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.TypeListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.ValidFlagListItem;
import egovframework.npqs.pqi.mobile.jsonparser.QuarantineEnblocDetailThread;
import egovframework.npqs.pqi.mobile.jsonparser.QuarantineEnblocDetailThread.QuarantineEnblocDetailThreadCallback;
import egovframework.npqs.pqi.mobile.jsonparser.QuarantineEnblocUpdateThread;
import egovframework.npqs.pqi.mobile.jsonparser.QuarantineEnblocUpdateThread.QuarantineEnblocUpdateThreadCallback;

//일괄결과등록
public class EnblocResultInsertActivity extends Activity implements OnClickListener 
	, QuarantineEnblocDetailThreadCallback
	, OnItemSelectedListener
	, QuarantineEnblocUpdateThreadCallback{
	private TextView titleTxt, textCnt, navigation;
	private AppStatus myapp;
	private Context context;
	private String checkStr, success, quranCd1, quranCd2;
	private ImageButton upBtn, downBtn, addBtn1, addBtn2, btnHome, btn_back;
	
	//본문 들어갈 자료들
	private Button footerBtn01, footerBtn02, footerBtn03, footerBtn04;
	private TextView content1_1, content1_2, headerTotTxt;
	private Spinner content2_1, content4_1;
	private ImageButton button2_1, button3_1, button4_1;
	private EditText  content3_1, content3_2, content5_1;
	private ListView listView;
	
	private QuarantineEnblocItem enblocItem;
	private QuarantineEnblocDetailThread enblocThread;
	private QuarantineEnblocListAdapter quarantineAdapter;
	private QuarantineEnblocUpdateThread enblocUpdateThread;
	
	private ArrayList<QuarantineDetailListItem> prodArrList;
	private ArrayList<ValidFlagListItem> validArrList;
	private ArrayList<DelaysayuListItem> delayArrList;
	private ArrayList<QunCdItem> quncdArrList;
	private ArrayList<TypeListItem> typeArrList;
	
	private ArrayAdapter<String> qunAdapter;
	private ArrayAdapter<String> typeAdapter;
	
	private ArrayList<String> spinDelayArrList, spinvalidArrList, spinQunArrList, spinTypeArrList;
	
	private String userSn1, userSn2;
	
	private final static int MSG_QUARANTINE_COMPLETED = 0;
	private final static int MSG_QUARANTINE_ERROR = 1;
	private final static int MSG_NETWORK_ERROR = 2;
	private final static int MSG_QUARANTINE_UPDATE_COMPLETED = 3;
	
	private boolean searchState;
	
	private boolean firstLoad;
	
	//하단 리스트 해더
	private LinearLayout list_header_Layout;
	
	//리스트 뷰
	private TextView enbloc_rec_no, enbloc_res_no,detail_list_seq, detail_list_trfclsf_cd;
	private TextView detail_list_itm_qty, detail_list_prod_name;
	private EditText detail_list_ins_qty;
	private Spinner detail_list_qun_cd, list_detail_spin;
	
	private String mResult;
	
	/* G-MOBILE 변수*/
    private String temp;
    private String requestNum;
    
    JSONObject resultDetail;
	JSONObject resultData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppStatus.navigation.addPathLocation("일괄결과등록");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_enbloc_result_insert);
		
		context = this;
		myapp = (AppStatus) getApplicationContext();
		
		firstLoad = true;
		quranCd1="";
		quranCd2="";
		
		Intent intent = getIntent();
		checkStr = intent.getStringExtra("checkStr");
		userSn1 = myapp.getLoginItem().getUserSn();
		init();
		
	}
	
	private void init(){
		myapp.startBroker(context);
		
		titleTxt = (TextView)findViewById(R.id.headerTitleText);
		titleTxt.setText("일괄결과등록");
		
		navigation = (TextView)findViewById(R.id.navigation);
		navigation.setText(AppStatus.navigation.getPath());
		Log.i("AppStatus.navigation.getPath()->", AppStatus.navigation.getPath());
		
		//하단 리스트 해더
		list_header_Layout = (LinearLayout)findViewById(R.id.list_header_Layout);
		list_header_Layout.setVisibility(View.VISIBLE);
		
		searchState = false;
		
		quarantineAdapter = new QuarantineEnblocListAdapter();
		
		footerBtn01 = (Button)findViewById(R.id.footer01Btn);
		footerBtn02 = (Button)findViewById(R.id.footer02Btn);
		footerBtn03 = (Button)findViewById(R.id.footer03Btn);
		footerBtn04 = (Button)findViewById(R.id.footer04Btn);
		
		content1_1 = (TextView)findViewById(R.id.content_1_1);
		content1_2 = (TextView)findViewById(R.id.content_1_2);
		
		content2_1 = (Spinner)findViewById(R.id.content_2_1);
		content4_1 = (Spinner)findViewById(R.id.content_4_1);
		
		button2_1 = (ImageButton)findViewById(R.id.button_2_1);
		button3_1 = (ImageButton)findViewById(R.id.button_3_1);
		button4_1 = (ImageButton)findViewById(R.id.button_4_1);
		
		content5_1 = (EditText)findViewById(R.id.content_5_1);
		content3_1 = (EditText)findViewById(R.id.content_3_1);
		content3_2 = (EditText)findViewById(R.id.content_3_2);
		
		listView = (ListView)findViewById(R.id.quarantine_list);
		
		addBtn1 = (ImageButton)findViewById(R.id.dosage_plus_btn1);
		addBtn2 = (ImageButton)findViewById(R.id.dosage_plus_btn2);
		
		/*loginBtn = (ImageButton)findViewById(R.id.btnLogin);
		loginBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myapp.appFinish(EnblocResultInsertActivity.this);				
			}
		});*/
		
		btn_back = (ImageButton)findViewById(R.id.btn_back);
		btn_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		btnHome = (ImageButton)findViewById(R.id.btnHome);
		btnHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myapp.setAbleHome(true);
				finish();
			}
		});
		
		//footerBtn01.setOnClickListener(this);
		footerBtn01.setBackgroundResource(R.drawable.f_blank_sel);
		footerBtn02.setOnClickListener(this);
		footerBtn03.setOnClickListener(this);
		footerBtn04.setOnClickListener(this);
		content3_1.setOnClickListener(this);
		content3_2.setOnClickListener(this);
		addBtn1.setOnClickListener(this);
		addBtn2.setOnClickListener(this);
		
	}
	
	private void firstDataLoad(){
		myapp.showDialogNo(context);
		temp = "처음검색";
		threadStart();
	}
	
	private void searchDetail(String requestNum){
		searchState = true;
		this.requestNum = requestNum;
		myapp.showDialogNo(context);	// 페이지가 로드 될때 다이얼로그 출력.
		temp = "저장검색";
		threadStart();
		
	}
	
	public void threadStart(){
		if("저장검색".equals(temp)){
    		enblocThread = new QuarantineEnblocDetailThread(context, this, checkStr, "377002"
    				, AppStatus.getmUserId());
    		enblocThread.start();
    	}
    	if("처음검색".equals(temp)){
    		enblocThread = new QuarantineEnblocDetailThread(context, this, checkStr, "000000"
    				, AppStatus.getmUserId());
    		enblocThread.start();
    	}
    	if("저장".equals(temp)){
    		enblocUpdateThread = new QuarantineEnblocUpdateThread(context, this
    				, resultData.toString(), resultDetail.toString(), AppStatus.getmUserId());
    		enblocUpdateThread.start();
    	}
	}
	
	private void layoutSetting() {
		content1_1.setText(enblocItem.getFieDe());
		content1_2.setText(enblocItem.getResultProgrsNm());
		if(firstLoad){
			if(enblocItem.getQuranNm1().toString().equals("")){
				userSn1 = myapp.getLoginItem().getUserSn();
				content3_1.setText(myapp.getLoginItem().getUserNm());
			}else{
				content3_1.setText(enblocItem.getQuranNm1());
			}
			content3_2.setText(enblocItem.getQuranNm2());
			firstLoad = false;
		}
		
		
		validArrList = enblocItem.getValidflagList();
		if(validArrList.size() > 0){
			spinvalidArrList = new ArrayList<String>();
			for (int i = 0; i < validArrList.size(); i++) {
				spinvalidArrList.add(validArrList.get(i).getCodeNm());
			}
		}
		
		delayArrList = enblocItem.getDelaysayuList();
		if(delayArrList.size() > 0){
			spinDelayArrList = new ArrayList<String>();
			for (int i = 0; i < delayArrList.size(); i++) {
				spinDelayArrList.add(delayArrList.get(i).getCodeNm());
			}
		}
		
		quncdArrList = enblocItem.getQunCdList();
		if(quncdArrList.size() > 0){
			spinQunArrList = new ArrayList<String>();
			for (int i = 0; i < quncdArrList.size(); i++) {
				spinQunArrList.add(quncdArrList.get(i).getCodeNm());
			}
		}
		
		typeArrList = enblocItem.getTypeList();
		if(typeArrList.size() > 0){
			spinTypeArrList = new ArrayList<String>();
			for (int i = 0; i < typeArrList.size(); i++) {
				spinTypeArrList.add(typeArrList.get(i).getCodeNm());
			}
		}
		
		ArrayAdapter<String> delayAdapter 
		= new ArrayAdapter<String>(context,  R.layout.spinner_item, spinDelayArrList);
		
		ArrayAdapter<String> validAdapter 
		= new ArrayAdapter<String>(context,  R.layout.spinner_item, spinvalidArrList);
		
		delayAdapter.setDropDownViewResource(R.layout.spinner_item);
		validAdapter.setDropDownViewResource(R.layout.spinner_item);
		
		content2_1.setAdapter(validAdapter);
		content4_1.setAdapter(delayAdapter);
		
		content5_1.setText(enblocItem.getRemark());
		
		for (int i = 0; i < validArrList.size(); i++) {
			if(validArrList.get(i).getCode().equals(enblocItem.getValidflag())){
				content2_1.setSelection(i);
			}
		}
		
		for (int i = 0; i < delayArrList.size(); i++) {
			if(delayArrList.get(i).getCode().equals(enblocItem.getPassResrveReasonCd())){
				content4_1.setSelection(i);
			}
		}
		content2_1.setOnItemSelectedListener(this);		
		content4_1.setOnItemSelectedListener(this);
		
		prodArrList = enblocItem.getItems();

		headerTotTxt = (TextView)findViewById(R.id.header_tot);
		upBtn = (ImageButton)findViewById(R.id.header_up_btn);
		downBtn = (ImageButton)findViewById(R.id.header_down_btn);

		headerTotTxt.setText("총 신청수:"+enblocItem.getItemsResult()+"건");
		listView.setAdapter(quarantineAdapter);
		
		upBtn.setOnClickListener(this);
		downBtn.setOnClickListener(this);
		downBtn.performClick();
		
	}
	
	private void requestSave(){
		resultDetail = new JSONObject();
		resultData = new JSONObject();
		JSONObject resultMaster = new JSONObject();
		JSONArray requestArray = new JSONArray();
		JSONObject obj;
		
		//유효성검사 현장검역관: content_3_1, content_3_2, 합격보류사유 : content_4_1
		if("".equals(content3_1.getText().toString())){
			myapp.utilm.ShowDialog(EnblocResultInsertActivity.this, "실패", "현장검역관을 입력 하세요.", 20);
			return;
		}
		if(null==content4_1.getSelectedItem()){
			myapp.utilm.ShowDialog(EnblocResultInsertActivity.this, "실패", "합격보유 사항을 선택 하세요.", 20);
			return;
		}
		
		for (int i = 0; i < prodArrList.size(); i++) {
			View v = quarantineAdapter.getView(i, null, listView);
			EditText editText=(EditText) v.findViewById(R.id.detail_list_ins_qty);
		    
			if(Float.parseFloat(prodArrList.get(i).getInsQty()) > Float.parseFloat(prodArrList.get(i).getItmQty())){
				myapp.utilm.ShowDialog(EnblocResultInsertActivity.this, "실패", "합격수량이 검역수량을 초과할 수 없습니다", 20);
				return;
			}
			
			View view = quarantineAdapter.getView(i, null, listView);
			EditText getText =(EditText) view.findViewById(R.id.detail_list_ins_qty);
			try {
				obj = new JSONObject();
				obj.put("offCd", prodArrList.get(i).getOffCd());
				obj.put("recNo", prodArrList.get(i).getRecNo());
				obj.put("recSeq", prodArrList.get(i).getRecSeq());
				obj.put("resOffCd", prodArrList.get(i).getResOffCd());
				obj.put("resNo", prodArrList.get(i).getResNo());
				obj.put("resSeq", prodArrList.get(i).getResSeq());
				obj.put("regType", prodArrList.get(i).getRegType());
				obj.put("msgId", prodArrList.get(i).getMsgId());
				obj.put("insQty", prodArrList.get(i).getInsQty());
				Log.i("insQty", prodArrList.get(i).getInsQty());
				obj.put("qunCd", prodArrList.get(i).getQunCd());
				obj.put("itemType", prodArrList.get(i).getItemType());
				obj.put("updNamCd", myapp.getLoginItem().getUserSn());
				requestArray.put(obj);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		try {
			Log.i("quran","quran = " + enblocItem.getQuranNamCd());
			resultMaster.put("validflag", enblocItem.getValidflag());
			resultMaster.put("passResrveReasonCd", enblocItem.getPassResrveReasonCd());
			resultMaster.put("remark", content5_1.getText().toString());
			/*resultMaster.put("quranNamCd1", myapp.getLoginItem().getUserSn());
	        resultMaster.put("quranNamCd2", enblocItem.getQuranNamCd());*/
			resultMaster.put("quranCd1", userSn1);
	        resultMaster.put("quranCd2", userSn2);
			resultMaster.put("searchRecNoList", checkStr);
			resultMaster.put("resultProgrsSt", "377002");
	        resultMaster.put("updNamCd", myapp.getLoginItem().getUserSn());
	        
	        resultData.put("resultMaster", resultMaster);
	        resultDetail.put("resultDetail", requestArray);
	        resultDetail.put("quranCd1", quranCd1);
	        resultDetail.put("quranCd2", quranCd2);
	        
	        Log.i("resultMaster",resultMaster.toString());
	        Log.i("resultDetail",requestArray.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		myapp.showDialogNo(context);
		temp = "저장";
		//asyncTaskIsConnectedVPN();
		threadStart();
		
	}
	
	@Override
	public void onClick(View v) {
		Intent intent ;
		switch (v.getId()) {
		case R.id.footer01Btn:	// 목록 버튼
			onBackPressed();
			break;
		case R.id.footer02Btn:	// 저장 버튼
			requestSave();
			break;
		case R.id.footer03Btn:	// 결재 버튼
			if(enblocItem.getResultProgrsSt().equals("377002") && enblocItem.getPassResrveReasonCd().equals("342001")){
				String fullResNo ="";
				for (int i = 0; i < prodArrList.size(); i++) {
					if(i == 0){
						fullResNo = prodArrList.get(i).getResOffCd()+"-"+prodArrList.get(i).getResNo();
					}else{
						fullResNo += ","+prodArrList.get(i).getResOffCd()+"-"+prodArrList.get(i).getResNo();
					}
				}
				intent = new Intent(EnblocResultInsertActivity.this, ApproveDetail.class);
				intent.putExtra("fullResNo", fullResNo);
				intent.putExtra("value", "1");
				startActivity(intent);
			}else{
				myapp.utilm.ShowDialog(EnblocResultInsertActivity.this, "실패", "저장을 먼저 해주세요.", 20);
			}
			break;
		case R.id.dosage_plus_btn1:
			Intent intent01 = new Intent(EnblocResultInsertActivity.this, PlaceOfficerActivity.class);
			intent01.putExtra("userNm", content3_1.getText().toString());
			startActivityForResult(intent01, 1);
			break;
			
		case R.id.dosage_plus_btn2:
			Intent intent02 = new Intent(EnblocResultInsertActivity.this, PlaceOfficerActivity.class);
			intent02.putExtra("userNm", content3_2.getText().toString());
			startActivityForResult(intent02, 2);
			break;
		
		case R.id.header_up_btn:
			RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams
		    (LayoutParams.WRAP_CONTENT, myapp.utilm.getDpToPixel(context, 400));
			listParams.addRule(RelativeLayout.ABOVE, R.id.bottom_layout);
			listView.setLayoutParams(listParams);
			break;
		case R.id.header_down_btn:
			RelativeLayout.LayoutParams listParams1 = new RelativeLayout.LayoutParams
		    (LayoutParams.WRAP_CONTENT, myapp.utilm.getDpToPixel(context, 95));
			listParams1.addRule(RelativeLayout.ABOVE, R.id.bottom_layout);
			listView.setLayoutParams(listParams1);
			break;		

		case R.id.content_3_1:
			content3_1.setText("");
			userSn1="";
			break;
			
		case R.id.content_3_2:
			content3_2.setText("");
			userSn2="";
			break;
			
		default:
			break;
		}
		
	}

	@Override
	public void OnQuarantineEnblocCompleted(boolean isNetworkConntected,
			QuarantineEnblocItem quarantineItem, String message) {
		if(null != message && !"".equals(message)){
			mResult = message;
			reesultToast();
		}
		
		if(myapp.dialog !=null && myapp.dialog.isShowing()) {
			myapp.dialog.dismiss();
		}
		
		if(!isNetworkConntected){	// 네트워크 접속이 되어 있지 않을 때
			Message msg = handler.obtainMessage();
			msg.what = MSG_NETWORK_ERROR;
			handler.sendMessage(msg);
			return;
		}
		
		if(quarantineItem != null) {
			this.enblocItem = quarantineItem;
			quncdArrList = enblocItem.getQunCdList();
			Message msg = handler.obtainMessage();
			msg.what = MSG_QUARANTINE_COMPLETED;
			handler.sendMessage(msg);
		}
		
	}

	@Override
	public void OnQuarantineEnblocError(ErrorItem errorItem) {
		if(myapp.dialog !=null && myapp.dialog.isShowing()) {
			myapp.dialog.dismiss();
		}

		Message msg = handler.obtainMessage();
		msg.what = MSG_QUARANTINE_ERROR;
		if(errorItem != null) {
			msg.obj = errorItem.getrDesc();
		}
		handler.sendMessage(msg);
		
	}
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(final Message msg) {
			switch (msg.what) {
			case MSG_QUARANTINE_COMPLETED:
				layoutSetting();
				break;

			case MSG_QUARANTINE_ERROR:
				String errMsg = "";
				if(msg == null ||msg.obj.toString().equals("")){
					errMsg = "서버와의 연결에 실패하였습니다.";
				}else{
					errMsg = msg.obj.toString();	
				}
				
				myapp.utilm.ShowDialog(EnblocResultInsertActivity.this, "실패", errMsg, 20);
				
				break;

			case MSG_NETWORK_ERROR:
				myapp.utilm.ShowDialog(EnblocResultInsertActivity.this, "실패", "네트워크에러", 20);

			case MSG_QUARANTINE_UPDATE_COMPLETED:
				new AlertDialog.Builder(EnblocResultInsertActivity.this)
				.setTitle("저장")
				.setMessage(success)
				.setPositiveButton("확인", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							searchDetail(prodArrList.get(0).getRecNo());
						} catch (Exception e) {

						}
					}
				})
				.setCancelable(false)
				.show();
				break;
			default:
				break;
			}
		}
	};
	
	class QuarantineEnblocListAdapter extends BaseAdapter {
		
		@Override
		public int getCount() {
			return prodArrList.size();
		}

		@Override
		public Object getItem(int position) {
			return prodArrList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null || convertView != null) {
    			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    			convertView = inflater.inflate(R.layout.list_enbloc_item, parent, false);
    		}
			
			enbloc_rec_no = (TextView)convertView.findViewById(R.id.enbloc_rec_no);
			enbloc_res_no = (TextView)convertView.findViewById(R.id.enbloc_res_no);
			detail_list_seq = (TextView)convertView.findViewById(R.id.detail_list_seq);
			detail_list_trfclsf_cd = (TextView)convertView.findViewById(R.id.detail_list_trfclsf_cd);
			detail_list_itm_qty = (TextView)convertView.findViewById(R.id.detail_list_itm_qty);
			detail_list_prod_name = (TextView)convertView.findViewById(R.id.detail_list_prod_name);
			detail_list_qun_cd = (Spinner)convertView.findViewById(R.id.detail_list_qun_cd);
			list_detail_spin = (Spinner)convertView.findViewById(R.id.list_detail_spin);
			detail_list_ins_qty = (EditText)convertView.findViewById(R.id.detail_list_ins_qty);
			
			enbloc_rec_no.setText(prodArrList.get(position).getOffCd() +"-"+prodArrList.get(position).getRecNo());
			enbloc_res_no.setText(prodArrList.get(position).getResOffCd() +"-"+prodArrList.get(position).getResNo());
			detail_list_seq.setText(prodArrList.get(position).getRecSeq());
			detail_list_trfclsf_cd.setText(prodArrList.get(position).getPrdlstCode());
			detail_list_itm_qty.setText(prodArrList.get(position).getItmQty());
			detail_list_prod_name.setText(prodArrList.get(position).getPrdlstNm());
			detail_list_ins_qty.setText(prodArrList.get(position).getInsQty());
			
			qunAdapter = new ArrayAdapter<String>(context,  R.layout.spinner_item, spinQunArrList);
			typeAdapter = new ArrayAdapter<String>(context,  R.layout.spinner_item, spinTypeArrList);
			detail_list_qun_cd.setAdapter(typeAdapter);
			list_detail_spin.setAdapter(qunAdapter);
			
			list_detail_spin.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int count, long id) {
					prodArrList.get(position).setQunCd(quncdArrList.get(count).getCode());
				}

				@Override
				public void onNothingSelected(AdapterView<?> parent) {}
			});
			
			detail_list_qun_cd.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int count, long id) {
					prodArrList.get(position).setItemType(typeArrList.get(count).getCode());
				}

				@Override
				public void onNothingSelected(AdapterView<?> parent) {}
			});
			
			for (int i = 0; i < quncdArrList.size(); i++) {
				if(quncdArrList.get(i).getCode().equals(prodArrList.get(position).getQunCd())){
					list_detail_spin.setSelection(i);
				}
			}
			
			for (int i = 0; i < typeArrList.size(); i++) {
				if(typeArrList.get(i).getCode().equals(prodArrList.get(position).getItemType())){
					detail_list_qun_cd.setSelection(i);
				}
			}
			
			detail_list_ins_qty.addTextChangedListener(new TextWatcher() {
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {}
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
				@Override
				public void afterTextChanged(Editable s) {
					prodArrList.get(position).setInsQty(s.toString());
				}
			});
			
			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					listItemClickEvent(position);
				}
			});
			
			return convertView;
		}
		
		private void listItemClickEvent(final int position) {
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		switch (parent.getId()) {
		case R.id.content_2_1:
			enblocItem.setValidflag(validArrList.get(position).getCode());
			break;

		case R.id.content_4_1:
			enblocItem.setPassResrveReasonCd(delayArrList.get(position).getCode());
			break;
			
		default:
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 1:
			if(resultCode == 1){
				content3_1.setText(data.getStringExtra("userNm"));
				userSn1 = data.getStringExtra("userSn");
				//enblocItem.setQuranNamCd(data.getStringExtra("userSn"));
			}
			break;
			
		case 2:
			if(resultCode == 1){
				content3_2.setText(data.getStringExtra("userNm"));
				userSn2 = data.getStringExtra("userSn");
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void OnQuarantineEnblocUpdateCompleted(boolean isNetworkConntected,
			String result, String message) {
		if(null != message && !"".equals(message)){
			mResult = message;
			reesultToast();
		}
		
		if(myapp.dialog !=null && myapp.dialog.isShowing()) {
			myapp.dialog.dismiss();
		}
		
		if(!isNetworkConntected){	// 네트워크 접속이 되어 있지 않을 때
			Message msg = handler.obtainMessage();
			msg.what = MSG_NETWORK_ERROR;
			handler.sendMessage(msg);
			return;
		}
		
		if(!result.equals("")) {
			this.success = result;
			//저장이완료되면 리로딩이 되어야 한다.
			myapp.setDataLoadState(true);
			
			Message msg = handler.obtainMessage();
			msg.what = MSG_QUARANTINE_UPDATE_COMPLETED;
			handler.sendMessage(msg);
		}
		
	}

	@Override
	public void OnQuarantineEnblocUpdatelError(ErrorItem errorItem) {
		Message msg = handler.obtainMessage();
		msg.what = MSG_QUARANTINE_ERROR;
		if(errorItem != null) {
			msg.obj = errorItem.getrDesc();
		}
		handler.sendMessage(msg);
		
	}
	
	@Override
	protected void onResume() {
		if(myapp.isFinishState()||myapp.isAbleHome()){
			finish();
		}else{
			myapp.startBroker(context);
			myapp.setDataDetailLoadState(false);
			firstDataLoad();
		}
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		AppStatus.navigation.delPathLocation();
	}
	
	private void reesultToast() {
		runOnUiThread(new Runnable() {

            public void run() {
                Toast toast = Toast.makeText(context, mResult, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 50);
                toast.show();
            }
        });
    }
	
}
